AVL树插入删除后的统一平衡算法(3+4重构)改写版

此算法改编自邓俊辉教授的《数据结构(C++语言版)》一书,原算法可参见原书或这篇博客:https://blog.csdn.net/wddpfx/article/details/82082724
鉴于原算法基于较多功能,我们平时使用AVL树时并不需要,特地改写一遍,希望各位指正。有BUG的话请留言。
以下算法下载链接 链接: https://pan.baidu.com/s/1dcpPvfEYB2L0iDdwnnMsRQ 密码: cx61

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

struct Node
{
    int data;
    int height;
    int level;

    Node* father, *lchild, *rchild;
};


Node* creatAVLTree(vector<int> vec);
void insert(Node* &root, int);
void deleteX(Node* root, int);
Node* findNode(Node* root, int x);
Node* findPreNode(Node* root);
Node* findSucNode(Node* root);

//统一平衡算法
bool isBalance(Node* root);
Node* reBalance(Node* root);
int updateHeight(Node* root);
Node* findBadSubTree(Node*);//先序遍历寻找失衡子树
Node* getSubBadTree(Node*);//查找失衡子树的较高孩子节点
void connect34(Node* a, Node* b, Node* c, Node* T0, Node* T1, Node* T2, Node* T3);

//层次遍历输出元素
void levelTraversal(Node* root);

int main()
{
    //vector<int> vec = { 0, 8, 10, 34, 88, 2, 4, 6, 1, 990, 1000, 99, 20, 19, 15, 79, 38, 666 };
    vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    Node* root = creatAVLTree(vec);
    deleteX(root, 2);
    levelTraversal(root);


    system("pause");
    return 0;
}
Node* creatAVLTree(vector<int> vec)
{
    Node* root = NULL;
    for (auto a : vec)
    {
        insert(root, a);
        //updateHeight(root);
        root = reBalance(root);
    }
    return root;
}
void insert(Node* &root, int x)
{
    if (root == NULL)//如果是根节点
    {
        root = new Node;
        root->data = x;
        root->height = 1;
        root->lchild = root->rchild = root->father = NULL;
        return;
    }

    if (root->data < x)
    {
        if (root->rchild == NULL)
        {
            Node* node = new Node;
            node->data = x;
            node->height = 1;
            node->lchild = node->rchild = node->father = NULL;
            root->rchild = node;//建立父子关系
            node->father = root;
        }
        else insert(root->rchild, x);
    }
    else if (x <= root->data)
    {
        if (root->lchild == NULL)
        {
            Node* node = new Node;
            node->data = x;
            node->height = 1;
            node->lchild = node->rchild = node->father = NULL;
            root->lchild = node;//建立父子关系
            node->father = root;
        }
        else insert(root->lchild, x);
    }
}
void deleteX(Node* root, int x)
{
    Node* node = findNode(root, x);//x所在的节点
    if (node == NULL)
    {
        printf("不存在此节点!\n");
        return;
    }
    Node* pre = findPreNode(node);//通过前驱节点来删除;
    Node* suc = findSucNode(node);//通过后继节点来删除;
    //Node* hot = node->father;

    if (node->lchild != NULL && node->rchild != NULL)//左右孩子均存在
    {
        swap(node->data, pre->data);
        node = pre;
    }
    Node* hot = node->father;
    if (node->lchild == NULL && node->rchild == NULL)
    {
        if (hot != NULL)
        {
            hot->lchild == node ? hot->lchild = NULL : hot->rchild = NULL;
        }
        delete node;
    }
    else if (node->lchild == NULL)//左孩子为空
    {
        if (hot != NULL)
        {
            hot->lchild == node ? hot->lchild = node->rchild : hot->rchild = node->rchild;
        }
        node->rchild->father = hot;
        delete node;
    }
    else if (node->rchild == NULL)//右孩子为空
    {
        if (hot != NULL)
        {
            hot->lchild == node ? hot->lchild = node->lchild : hot->rchild = node->lchild;
        }
        node->lchild->father = hot;
        delete node;
    }
    reBalance(root);
    //updateHeight(root);
}
Node* findNode(Node* root, int x)
{
    if (root == NULL || root->data == x) return root;
    if (root->data < x) return findNode(root->rchild, x);
    else return findNode(root->lchild, x);
}
Node* findPreNode(Node* root)
{
    if (root == NULL || root->lchild == NULL) return NULL;
    Node* node = root->lchild;
    while (node->rchild != NULL) node = node->rchild;
    return node;
}
Node* findSucNode(Node* root)
{
    if (root == NULL || root->rchild == NULL) return NULL;
    Node* node = root->rchild;
    while (node->lchild != NULL) node = node->lchild;
    return node;
}

bool isBalance(Node* root)
{
    if (root->lchild == NULL && root->rchild == NULL) return true;
    else if (root->lchild == NULL && root->rchild != NULL)return root->rchild->height <= 1;
    else if (root->lchild != NULL && root->rchild == NULL) return root->lchild->height <= 1;
    else return abs(root->rchild->height - root->lchild->height) <= 1;
}
Node* reBalance(Node* root)
{
    Node *x, *y, *z, *p;//p为第一个非平衡子树根节点的父节点
    x = y = z = NULL;
    x = findBadSubTree(root);
    if (x == NULL)
    {
        cout << "AVL树已经平衡" << endl;
        return root;
    }
    p = x->father;
    y = getSubBadTree(x);
    if (y == NULL) cout << "y出错啦!" << endl;
    z = getSubBadTree(y);
    if (z == NULL) cout << "z出错啦!" << endl;

    Node *a, *b, *c, *T0, *T1, *T2, *T3;
    if (y == x->lchild)
    {
        if (z == y->lchild)
        {
            a = z;
            b = y;
            c = x;
            T0 = z->lchild;
            T1 = z->rchild;
            T2 = y->rchild;
            T3 = a->rchild;
        }
        else
        {
            a = y;
            b = z;
            c = x;
            T0 = y->lchild;
            T1 = z->lchild;
            T2 = z->rchild;
            T3 = x->rchild;
        }
    }
    else
    {
        if (z == y->rchild)
        {
            a = x;
            b = y;
            c = z;
            T0 = x->lchild;
            T1 = y->lchild;
            T2 = z->lchild;
            T3 = z->rchild;
        }
        else
        {
            a = x;
            b = z;
            c = y;
            T0 = x->lchild;
            T1 = z->lchild;
            T2 = z->rchild;
            T3 = y->rchild;
        }
    }
    connect34(a, b, c, T0, T1, T2, T3);
    //if(x->father == NULL)
    if (p == NULL)
    {
        b->father = NULL;
        root = b;
    }
    else if (x == p->lchild)
    {
        p->lchild = b;
        b->father = p;
    }
    else
    {
        p->rchild = b;
        b->father = p;
    }
    updateHeight(root);
    return root;
}
int updateHeight(Node* root)
{
    if (root->lchild == NULL && root->rchild == NULL)
    {
        root->height = 1;
        return root->height = 1;
    }
    else if (root->lchild == NULL && root->rchild != NULL) root->height = updateHeight(root->rchild) + 1;
    else if (root->lchild != NULL && root->rchild == NULL) root->height = updateHeight(root->lchild) + 1;
    else root->height = max(updateHeight(root->lchild), updateHeight(root->rchild)) + 1;
}
Node* findBadSubTree(Node* root)
{
    updateHeight(root);
    if (root == NULL) return NULL;
    stack<Node*> sta;
    while (true)
    {
        if (isBalance(root) == false)
        {
            return root;
        }
        if (root->rchild != NULL) sta.push(root->rchild);
        if (root->lchild != NULL) sta.push(root->lchild);
        if (sta.empty()) break;
        root = sta.top();
        sta.pop();
    }
    return NULL;
}
Node* getSubBadTree(Node* root)
{
    if (root->lchild != NULL && root->rchild != NULL)
    {
        return root->lchild->height > root->rchild->height ? root->lchild : root->rchild;
    }
    else if (root->lchild != NULL && root->rchild == NULL) return root->lchild;
    else if (root->lchild == NULL && root->rchild != NULL) return root->rchild;
    return NULL;
}
void connect34(Node* a, Node* b, Node* c, Node* T0, Node* T1, Node* T2, Node* T3)
{
    b->lchild = a, a->father = b;
    b->rchild = c, c->father = b;
    a->lchild = T0; if (T0 != NULL) T0->father = a;
    a->rchild = T1; if (T1 != NULL) T1->father = a;
    c->lchild = T2; if (T2 != NULL) T2->father = c;
    c->rchild = T3; if (T3 != NULL) T3->father = c;
}

//输出
void levelTraversal(Node* root)
{
    queue<Node*> Q;
    Q.push(root);
    root->level = 1;
    int nowLevel = root->level;
    printf("第%d层:\n", nowLevel);
    while (!Q.empty())
    {
        Node* front = Q.front();
        Q.pop();
        int level = front->level;
        if (level != nowLevel)
        {
            nowLevel = level;
            printf("\n第%d层:\n", nowLevel);
        }
        printf("%d height = %d\n", front->data, front->height);
        if (front->lchild != NULL)
        {
            Q.push(front->lchild);
            front->lchild->level = level + 1;
        }
        if (front->rchild != NULL)
        {
            Q.push(front->rchild);
            front->rchild->level = level + 1;
        }
    }
    printf("\n");
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: AVL树是一种自平衡二叉搜索树。它的插入删除过程都有一定的复杂度。 在 AVL树插入新节点时,需要先找到新节点应该插入的位置,然后将新节点插入到该位置。如果在插入新节点后AVL树平衡性被破坏,则需要对AVL树进行调整,以使其重新平衡。这个过程需要使用旋转操作来调整AVL树的结构。 在AVL树删除节点时,需要先找到要删除的节点,然后在AVL树删除该节点。如果在删除节点后AVL树平衡性被破坏,则需要对AVL树进行调整,以使其重新平衡。这个过程也需要使用旋转操作来调整AVL树的结构。 总的来说,AVL树插入删除都比较复杂,但是它们可以保证AVL树在进行插入删除操作后仍然是一棵平衡树。 ### 回答2: AVL树是一种自平衡二叉搜索树,插入删除操作会涉及到树的平衡调整,因此相对来说复杂一些。 在AVL树插入操作,首先会按照二叉搜索树的规则找到插入位置。在插入新节点后,需要从插入位置开始向上逐层检查是否平衡,并适时进行旋转操作来恢复平衡。旋转操作有四种,分别为左旋、右旋、左右旋和右左旋。当需要旋转时,会通过旋转调整节点的左右子树来维持平衡。 在AVL树删除操作,也需要按照二叉搜索树的规则找到要删除的节点。删除节点后,同样需要从删除节点的父节点开始向上逐层检查是否平衡,并进行旋转操作来恢复平衡。与插入操作类似,删除操作也可能需要进行左旋、右旋、左右旋或右左旋。 因为插入删除操作都会引起树的平衡性发生改变,所以需要对整棵树进行频繁的旋转操作。这就增加了算法的复杂性,导致插入删除操作相对困难。 但正是由于AVL树的自平衡特性,保证了树的平衡性,使得在搜索、插入删除等操作的时间复杂度都能够保持在O(logn)的级别,这样在大规模数据的处理中仍然是非常高效的。因此,虽然插入删除操作相对较难,但AVL树的特性使其成为一种非常重要和有价值的数据结构。 ### 回答3: AVL树是一种自平衡二叉搜索树,相比于普通的二叉搜索树,它可以在插入删除节点时保持树的平衡状态,以提供更高效的查找、插入删除操作。 AVL树插入操作相对复杂一些,因为插入新节点可能会导致树失去平衡插入一个节点的过程涉及对根节点到插入位置之间的路径上的每个节点进行旋转操作以调整树的平衡。这涉及到计算每个节点的平衡因子,即左子树的高度减去右子树的高度。如果插入节点后某个节点的平衡因子大于1或小于-1,需要进行旋转操作来恢复平衡AVL树删除操作比插入稍微复杂一些。删除一个节点后,也可能导致树失去平衡。与插入不同,删除操作可能导致更多的旋转操作,以恢复树的平衡。在删除节点之后,需要根据被删除节点的后继(或前驱)节点来替代被删除节点的位置,并更新路径上的每个节点的平衡因子。与插入一样,如果某个节点的平衡因子大于1或小于-1,需要进行旋转操作来恢复平衡。 总结起来,AVL树插入删除操作相对难以理解和实现,因为它涉及到计算平衡因子和多次旋转操作。对于插入删除而言,我们需要确保在执行这些操作后,树的高度仍然保持较小,并且保持节点的平衡。因此,在实际应用中,为了简化操作和提高性能,我们可能会使用其他平衡树的数据结构,如红黑树。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值